//
//  JFBlowFreshDecode.m
//  chengyuwar
//
//  Created by ran on 14-1-14.
//  Copyright (c) 2014年 com.lelechat.chengyuwar. All rights reserved.
//

#import "JFBlowFreshDecode.h"
#import "NSData+JFEncodeData.h"

#ifndef BLOWFRESHPWD
#define BLOWFRESHPWD           @"3b145d00529bd3f9c488843b84b06fe7"
#endif

@implementation JFBlowFreshDecode


+ (BOOL)configDefaultPwd:(NSString*)configPwd
{
    if (configPwd){
        [[NSUserDefaults standardUserDefaults] setObject:configPwd forKey:@"configPwd"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    return YES;
}

+ (NSString*)configPwd
{
    NSString *configPwd = [[NSUserDefaults standardUserDefaults] objectForKey:@"configPwd"];
    if (configPwd == nil) {
        configPwd = BLOWFRESHPWD;
    }
    return configPwd;
}
+ (NSData*)getNopwdDataAccordFilePath:(NSString*)strPath
{
    
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:strPath])
    {
        NSLog(@"strPath file is not exist :%@",strPath);
        return nil;
    }
    NSError *error = nil;
    
    NSData *data = [NSData dataWithContentsOfFile:strPath options:NSDataReadingMappedIfSafe error:&error];

    return data;
    
}
+ (NSData*)getDataAccordFilePath:(NSString*)strPath
{
    
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:strPath])
    {
        NSLog(@"strPath file is not exist :%@",strPath);
        return nil;
    }
    NSError *error = nil;
    
    NSData *data = [NSData dataWithContentsOfFile:strPath options:NSDataReadingMappedIfSafe error:&error];
    NSData *imgData1 = [data AES256DecryptWithKey:[self configPwd]];
   
    return imgData1;
    
}

+ (BOOL)WriteDataToFilePath:(NSString*)strPath data:(NSData*)data
{

    NSError *error = nil;
 
   // popo_bf_decrypt(buf, data.length);
    NSData *imgData1 = [data AES256EncryptWithKey:[self configPwd]];

    if ([[NSFileManager defaultManager] fileExistsAtPath:strPath])
    {
    
        error = nil;
        BOOL bsuc = [[NSFileManager defaultManager] removeItemAtPath:strPath error:&error];
    
        if (!bsuc && error)
        {
            NSLog(@"removeItemAtPath:%@ error:",strPath);
            return NO;
           
        }
        
     
        
    }
    
    error = nil;
    NSString    *strDoc = [strPath stringByDeletingLastPathComponent];
    if (![[NSFileManager defaultManager] fileExistsAtPath:strDoc])
    {
        BOOL bsuc = [[NSFileManager defaultManager] createDirectoryAtPath:strDoc withIntermediateDirectories:YES attributes:nil error:&error];
        
        if (!bsuc && error)
        {
            NSLog(@"createDirectoryAtPath:%@  error:%@",strDoc,error);
            return NO;
        }
    }
    
    
    BOOL   bsuc =  [imgData1 writeToFile:strPath atomically:YES];
    
    if (!bsuc)
    {
        NSLog(@"writeToFile %@ fail",strPath);
        return NO;
    }
    
    return YES;
    
}


+ (BOOL)WriteDataNoPwdToFilePath:(NSString*)strPath data:(NSData*)data
{
    
    NSError *error = nil;
    if ([[NSFileManager defaultManager] fileExistsAtPath:strPath])
    {
        error = nil;
        BOOL bsuc = [[NSFileManager defaultManager] removeItemAtPath:strPath error:&error];
        
        if (!bsuc && error)
        {
            NSLog(@"removeItemAtPath:%@ error:",strPath);
            return NO;
            
        }
        
        
        
    }
    
    error = nil;
    NSString    *strDoc = [strPath stringByDeletingLastPathComponent];
    if (![[NSFileManager defaultManager] fileExistsAtPath:strDoc])
    {
        BOOL bsuc = [[NSFileManager defaultManager] createDirectoryAtPath:strDoc withIntermediateDirectories:YES attributes:nil error:&error];
        
        if (!bsuc && error)
        {
            NSLog(@"createDirectoryAtPath:%@  error:%@",strDoc,error);
            return NO;
        }
    }
    
    
    BOOL   bsuc =  [data writeToFile:strPath atomically:YES];
    
    if (!bsuc)
    {
        NSLog(@"writeToFile %@ fail",strPath);
        return NO;
    }
    
    return YES;
    
}


+ (BOOL)writeDataFromPath:(NSString*)strOldPath newPath:(NSString*)newPath
{
    NSData  *data = [JFBlowFreshDecode getNopwdDataAccordFilePath:strOldPath];
    if (data)
    {
        if ([JFBlowFreshDecode WriteDataToFilePath:newPath data:data])
        {
            return YES;
        }else
        {
            NSLog(@"writeDataFromPath:%@ fail",strOldPath);
            return NO;
        }
    }
    
    
    return NO;
}
@end
